package com.rs.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.rs.entities.ResultBean;
import com.rs.service.UploadService;

import net.sf.json.JSONObject;

public class UploadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private UploadService uploadService;

	public void setUploadService(UploadService uploadService) {
		this.uploadService = uploadService;
	}

	private File file;
	private String fileContentType;
	private String fileFileName;

	private String data;

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	private JSONObject result;

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	@Override
	public String toString() {
		return "UploadAction [file=" + file + ", fileContentType=" + fileContentType + ", fileFileName=" + fileFileName
				+ "]";
	}

	public String uploadFile() {
		System.out.println("uploadData" + data);
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		ServletContext context = ServletActionContext.getServletContext();
		String upload_pic = context.getRealPath("/upload_pic/");
		if (fileFileName == null) {
			fileFileName = System.currentTimeMillis() + ".jpg";
		}
		File outFile = null;
		if (file != null) {
			outFile = new File(upload_pic + fileFileName);
		}
		if (!outFile.exists()) {
			outFile.getParentFile().mkdir();
		}
		ResultBean resultBean = null;
		String savePath = "";

		try {
			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(new FileOutputStream(outFile));
			int len = 0;
			byte[] buffer = new byte[10240];
			while ((len = bis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			savePath = uploadService.savePic(data, fileFileName);
			resultBean = new ResultBean(200, "success", savePath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			resultBean = new ResultBean(400, "failure", "服务器找不到相应文件");
		} catch (IOException e) {
			e.printStackTrace();
			resultBean = new ResultBean(401, "failure", "服务器读取文件出错");
		} catch (Exception e) {
			e.printStackTrace();
			resultBean = new ResultBean(401, "failure", "服务器保存文件出错");
		} finally {

			try {
				if (bis != null) {
					bis.close();
				}
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				resultBean = new ResultBean(200, "success", savePath);
				e.printStackTrace();
			}

		}
		result = JSONObject.fromObject(resultBean);
		System.out.println("uploadResult:" + result);
		return "uploadFile";
	}

}
